package com.gdth.bank.model;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppBfrjBfjl;
import com.gdth.database.bean.TAppBfrjBfjlDao;

import org.greenrobot.greendao.query.QueryBuilder;


public class CjkhBfrjModel {
	/**
	 * 查询当前采访客户的所有拜访日记
	 * @param
	 * @return
	 */
	public Observable<List<TAppBfrjBfjl>> getBfjlByUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjBfjl>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjBfjl>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjBfjlDao dao = BankApplication.getDaoSession().getTAppBfrjBfjlDao();
				QueryBuilder<TAppBfrjBfjl> qb = dao.queryBuilder();

				qb.where(TAppBfrjBfjlDao.Properties.Jcuuid.like(jcuuid),
						TAppBfrjBfjlDao.Properties.Sjzt.notEq(AppUtil.sjzt_cancel)).orderDesc(TAppBfrjBfjlDao.Properties.Id);

				List<TAppBfrjBfjl> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 采访客户基本信息保存
	 * @param mTAppBfrjBfjl
	 * @return
	 */
	public Observable<Long> saveCjkhjbxx(TAppBfrjBfjl mTAppBfrjBfjl){
		final TAppBfrjBfjl tAppBfrjBfjl = mTAppBfrjBfjl;
		return Observable.create(new Observable.OnSubscribe<Long>() {

			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjBfjlDao dao = session.getTAppBfrjBfjlDao();
						/*Query query = dao.queryBuilder().where(new StringCondition("date("+TAppBfrjBfjlDao.Properties.Cjsj+") = date('now')")).build();
						List  list = query.list();*/
						try {
							dao.insertOrReplace(tAppBfrjBfjl);
							CJkhxxModel.updateCjkhJbxxZt(tAppBfrjBfjl.getJcuuid());
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							TAppBfrjBfjl mTAppBfrjBfjl	=tAppBfrjBfjl;
							arg0.onNext(tAppBfrjBfjl.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}


	public Observable<Long> deletebfjl(TAppBfrjBfjl mTAppBfrjBfjl){
		return Observable.create(new Observable.OnSubscribe<Long>() {

			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO 采访客户的资产信息
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjBfjlDao dao = session.getTAppBfrjBfjlDao();

						try {
							mTAppBfrjBfjl.setSjzt(AppUtil.sjzt_delete);
							dao.insertOrReplace(mTAppBfrjBfjl);
							CJkhxxModel.updateCjkhJbxxZt(mTAppBfrjBfjl.getJcuuid());
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							arg0.onNext(mTAppBfrjBfjl.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
}
